using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using Ciripescu_WebRole.Properties;

namespace EmailSender
{
    

    public class MailSender
    {
        
        #region Public Methods

        /// <summary>
        /// Sends an email to the indicated destination using default email.
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <exception cref="Exception"></exception>
        public static void Send(string to, string subject, string body)
        {
            SmtpClient client;
            MailMessage message;
            
            try
            {
                // client = new SmtpClient();

                client = new SmtpClient(Settings.Default.Hostname,
                                        25);

                

                client.DeliveryMethod        = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(Settings.Default.Account, Settings.Default.Password);  

                MailAddress from = new MailAddress(Settings.Default.DefaultEmailAddr,
                                                   Settings.Default.DefaulDisplayName);
                var split = to.Split('/');
                MailAddress too = new MailAddress(split[1], split[0]);
                
                message         = new MailMessage(from,too);
                message.Body    = body;
                message.Subject = subject;
                // cred ca e mai bine sa nu trimitem html-uri
                message.IsBodyHtml = false;

                client.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Send a message from an expeditor to a receiver.
        /// </summary>
        /// <param name="from">The expeditor of the message.</param>
        /// <param name="to">The receiver of the message.</param>
        /// <param name="subject">The subject of the message.</param>
        /// <param name="body">The body of the message.</param>
        /// <exception cref="Exception"></exception>
        public static void Send(string from,
                                string to,
                                string subject,
                                string body)
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(from);
            msg.To.Add(new MailAddress(to));
            msg.Subject = subject;
            msg.Body = body;
            msg.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();

            //client.UseDefaultCredentials = true;
            client.Send(msg);
        }

        #endregion

        #region Public Properties
        #endregion

        #region Public Delegates
        #endregion

        #region Protected Methods
        #endregion

        #region Private Methods
        #endregion

        #region Protected Members
        #endregion

        #region Private Members
        #endregion
    }
}
